﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class LotAttributeCodeBLL : IBaseInfoDetail
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly ILotAttributeCodeDA lotAttributeCodelDA = DataAccess.CreateLotAttributeCodeDA();
        private PubParasInfo pubParasInfo;

        public LotAttributeCodeBLL()
        {
            this.pubParasInfo = null;
        }

        public LotAttributeCodeBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public LotAttributeCodeBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> mainID, List<string> subId, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if (mainID.Count != subId.Count)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2610_002", this.pubParasInfo.UserLanguage);
                }
                List<LotAttributeCodeInfo> lotAttributeCodeInfos = new List<LotAttributeCodeInfo>();
                LotAttributeCodeInfo lotAttributeCodeInfo = null;
                for (i = 0; i < mainID.Count; i++)
                {
                    lotAttributeCodeInfo = lotAttributeCodelDA.GetLotAttributeCodeByID(dataBase, tran, mainID[i], subId[i], this.dataBaseID[0]);
                    lotAttributeCodeInfos.Add(lotAttributeCodeInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                lotAttributeCodelDA.DeleteLotAttributeCode(dataBase, tran, mainID, subId, this.dataBaseID);
                for (i = 0; i < mainID.Count; i++)
                {
                    auditDataBLL.InsertAuditData<LotAttributeCodeInfo>(dataBase, tran, lotAttributeCodeInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "LOT_ATTRIBUTE_CODE", this.dataBaseID);
                    string CS00002 = temp;
                    temp = CS00002 + mainID[i] + "," + subId[i] + ";";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return lotAttributeCodelDA.GetAllLotAttributeCode(this.dataBaseID[0]);
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            LotAttributeCodeQueryEntity lotAttributeQuery = obj as LotAttributeCodeQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(lotAttributeQuery.GroupID))
            {
                sqlWhere.Add(" GROUP_ID " + lotAttributeQuery.GroupID);
            }
            if (!string.IsNullOrEmpty(lotAttributeQuery.CodeID))
            {
                sqlWhere.Add(" CODE_ID " + lotAttributeQuery.CodeID);
            }
            if (!string.IsNullOrEmpty(lotAttributeQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + lotAttributeQuery.IsActive);
            }
            return lotAttributeCodelDA.GetLotAttributeCodeByQueryList(sqlWhere, lotAttributeQuery, this.dataBaseID[0]);
        }

        public object GetDetailInfoByID(string mainID, string subID, string pk3)
        {
            return lotAttributeCodelDA.GetLotAttributeCodeByID(mainID, subID, this.dataBaseID[0]);
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            List<LotAttributeCodeInfo> lotAttributeCodeInfoDetails = lotAttributeCodelDA.GetLotAttributeCodeListByID(mainID, this.dataBaseID[0]);
            List<object> objs = null;
            if ((lotAttributeCodeInfoDetails != null) && (lotAttributeCodeInfoDetails.Count > 0))
            {
                objs = new List<object>();
                for (int i = 0; i < lotAttributeCodeInfoDetails.Count; i++)
                {
                    objs.Add(lotAttributeCodeInfoDetails[i]);
                }
            }
            return objs;
        }

        public string InsertDetail(object obj)
        {
            LotAttributeCodeInfo lotAttributeCodeInfo = obj as LotAttributeCodeInfo;
            string codeID = string.Empty;
            try
            {
                if (lotAttributeCodelDA.CheckLotAttributeCodeIDUnique(lotAttributeCodeInfo.GroupID, lotAttributeCodeInfo.CodeID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2610_001", this.pubParasInfo.UserLanguage, new List<string> { lotAttributeCodeInfo.GroupID, lotAttributeCodeInfo.CodeID });
                }
                if (lotAttributeCodeInfo == null)
                {
                    return codeID;
                }
                lotAttributeCodeInfo.UpdatedBy = lotAttributeCodeInfo.CreatedBy;
                lotAttributeCodeInfo.CreatedDate = DateTime.Now;
                lotAttributeCodeInfo.UpdatedDate = DateTime.Now;
                if (lotAttributeCodelDA.InsertLotAttributeCode(lotAttributeCodeInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<LotAttributeCodeInfo>(null, lotAttributeCodeInfo, AuditAction.Insert, this.pubParasInfo.UserID, "LOT_ATTRIBUTE_CODE", this.dataBaseID[0]);
                    codeID = lotAttributeCodeInfo.CodeID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return codeID;
        }

        public string UpdateDetail(object obj)
        {
            string codeID = string.Empty;
            try
            {
                LotAttributeCodeInfo codeDetail = obj as LotAttributeCodeInfo;
                LotAttributeCodeInfo oldLotAttributeCodeInfo = lotAttributeCodelDA.GetLotAttributeCodeByID(codeDetail.CodeID, codeDetail.GroupID, this.dataBaseID[0]);
                if (lotAttributeCodelDA.UpdateLotAttributeCode(codeDetail, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<LotAttributeCodeInfo>(oldLotAttributeCodeInfo, codeDetail, AuditAction.Update, this.pubParasInfo.UserID, "LOT_ATTRIBUTE_CODE", this.dataBaseID[0]);
                    codeID = codeDetail.GroupID + "," + codeDetail.CodeID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return codeID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

